﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 护士工作站
{
    public partial class 转出 : Form
    {
        public 转出()
        {
            InitializeComponent();
        }





     
        private void button_qp_Click(object sender, EventArgs e)
        {
            tb_bq.Text = "";
            tb_ch.Text = "";
            tb_rzks.Text = "";
            tb_nl.Text = "";
            tb_ryrq.Text = "";
            cb_zzbq.Text = "";
            tb_xb.Text = "";
            tb_xm.Text = "";
            tb_yjje.Text = "";
            tb_ys.Text = "";
            tb_fyhj.Text = "";
            tb_czy.Text = "";
            tb_ZYNo.Text = "";
            tb_hldj.Text = "";
            dtp_zksj.Text = "";
        }

        private void button_cx_Click(object sender, EventArgs e)
        {
            string ZYNo = tb_ZYNo.Text.Trim();
            string ch = tb_ch.Text.Trim();

            if (ZYNo == "" && ch == "")
            {
                MessageBox.Show("请输入要查询的内容！");
                tb_ZYNo.Focus();
                tb_ch.Focus();

                return;
            }


            string Hos = "select * from InHospital,Patient,Bed where InHospital.HosNo='" + ZYNo + "'and InHospital.HosNo=Patient.HosNo and InHospital.BedNo=Bed.BedNo";
            string Bed = "select * from Bed ,InHospital,Patient where Bed .BedNo='" + ch + "'and InHospital.HosNo=Patient.HosNo and InHospital.BedNo=Bed.BedNo";

            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {

                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                SqlCommand cmd = new SqlCommand(Hos, conn);
                SqlCommand str = new SqlCommand(Bed, conn);

                if (ZYNo != "")
                {
                    SqlDataReader dr = cmd.ExecuteReader();

                    if (dr.HasRows)
                    {
                        dr.Read();
                        tb_bq.Text = dr["HosCondition"].ToString();
                        tb_fyhj.Text = dr["HosTotal"].ToString();

                        int nf = Convert.ToInt32(dr["BronYear"].ToString());
                        DateTime time = System.DateTime.Now;
                        int year = time.Year;
                        int nl = year - nf;
                        tb_nl.Text = nl.ToString();
                        tb_ryrq.Text = dr["HosIndate"].ToString();


                        tb_xb.Text = dr["PatSex"].ToString();
                        tb_xm.Text = dr["PatName"].ToString();
                        tb_ch.Text = dr["BedNo"].ToString();
                        tb_rzks.Text = dr["HosDepartment"].ToString();

                        tb_yjje.Text = dr["HosPrepay"].ToString();

                        tb_ZYNo.Text = dr["HosNo"].ToString();
                        tb_hldj.Text = dr["HosRank"].ToString();

                        string dno = dr["DocNo"].ToString();

                        dr.Close();

                        string DNo = "select DocName from Doctor where  DocNo='" + dno + "' ";
                        SqlCommand D = new SqlCommand(DNo, conn);

                        string DD = Convert.ToString(D.ExecuteScalar());

                        tb_ys.Text = DD;

                    }
                    dr.Close();
                }

                if (ch != "")
                {
                    SqlDataReader ds = str.ExecuteReader();
                    if (ds.HasRows)
                    {
                        ds.Read();
                        tb_bq.Text = ds["HosCondition"].ToString();
                        tb_fyhj.Text = ds["HosTotal"].ToString();

                        int nf = Convert.ToInt32(ds["BronYear"].ToString());
                        DateTime time = System.DateTime.Now;
                        int year = time.Year;
                        int nl = year - nf;
                        tb_nl.Text = nf.ToString();
                        tb_ryrq.Text = ds["HosIndate"].ToString();


                        tb_xb.Text = ds["PatSex"].ToString();
                        tb_xm.Text = ds["PatName"].ToString();
                        tb_ch.Text = ds["BedNo"].ToString();
                        tb_rzks.Text = ds["HosDepartment"].ToString();

                        tb_yjje.Text = ds["HosPrepay"].ToString();

                        tb_ZYNo.Text = ds["HosNo"].ToString();
                        tb_hldj.Text = ds["HosRank"].ToString();

                        string dno = ds["DocNo"].ToString();

                        ds.Close();

                        string DNo = "select DocName from Doctor where  DocNo='" + dno + "' ";
                        SqlCommand D = new SqlCommand(DNo, conn);

                        string DD = Convert.ToString(D.ExecuteScalar());

                        tb_ys.Text = DD;

                    }
                    ds.Close();
                }

            }
        }

   
     

        private void button_tc_Click(object sender, EventArgs e)
        {
            this.Hide();
            菜单页面 f = new 菜单页面();
            f.Show();
        }
       
        private void 转出_Load(object sender, EventArgs e)
        {

        }

        private void cb_zzbq_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string NurNo = UserHelper.NurNo;
                string sql = "select NurName from Nurse where NurNo='" + NurNo + "'";
                conn.Open();
                SqlCommand nur = new SqlCommand(sql, conn);
                string n= Convert.ToString(nur.ExecuteScalar());
                tb_czy.Text = n;
                conn .Close ();
            }
        }

        private void button_bc_Click(object sender, EventArgs e)
        {
            string bedNo = tb_ch.Text.Trim();
            string hosNo = tb_ZYNo.Text.Trim();
            DateTime HosIndate = Convert.ToDateTime(this.tb_ryrq.Text);
            DateTime TurnInWardTime = this.dtp_zksj.Value;
            string TurnInArea = cb_zzbq.Text.Trim();
            string Operator = tb_czy.Text.Trim(); 
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {

                string str = string.Format("insert into TurnInWard2( HosNo, BedNo, HosIndate, HosDepartment, HosPrepay, PatType, HosSurgerydate, HosRank, HosCondition, HosIllness,HosTotal,HosBalance,DocNo,NurNo,HosOfficeDate,HosArea)select HosNo, BedNo, HosIndate, HosDepartment, HosPrepay, PatType, HosSurgerydate, HosRank, HosCondition, HosIllness,HosTotal,HosBalance,DocNo,NurNo,HosOfficeDate,HosArea from InHospital where InHospital.HosNo='" + hosNo + "'");
                string sql = string.Format("insert into TurnInWard1 values ('{0}','{1}','{2}','{3}')", hosNo, TurnInArea, TurnInWardTime, Operator);
                string str1 = string.Format("delete from InHospital  where InHospital.HosNo='" + hosNo + "'");
                string ssql = string.Format("update Bed set BedCondition='未用'where Bed.BedNo='" + bedNo + "'");
                string sql1 = string.Format("update Patient set Condition='转出'where Patient.HosNo='" + hosNo + "'");
                SqlCommand com = new SqlCommand(str, conn);
                SqlCommand bed = new SqlCommand(ssql, conn);
                SqlCommand ins = new SqlCommand(sql, conn);
                SqlCommand dein = new SqlCommand(str1, conn);
                SqlCommand pat = new SqlCommand(sql1, conn);
                conn.Open();
                int m = Convert.ToInt32(com.ExecuteNonQuery());
                int n = Convert.ToInt32(dein.ExecuteNonQuery());
                int z = Convert.ToInt32(bed.ExecuteNonQuery());
                int y = Convert.ToInt32(ins.ExecuteNonQuery());
                int a = Convert.ToInt32(pat.ExecuteNonQuery());
               
                    if (y == 1 && n == 1 && z == 1&&m==1&&a==1)
                    {
                        MessageBox.Show("转出成功！");
                    }
                    else
                    {
                        MessageBox.Show("转出失败！");
                    }
                }
              }
            }

        }
      




